﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StripeOne.Core.S1Tools;
using System.Globalization;

namespace StripeOne.Core.S1Config
{
    public class S1Config
    {
        private enum S1ConfigType
		{
			File,
			Http
		}
		private string _configFile;
		private string _configSection;
		private bool _inheritFromDefault;
		private bool _useCache;
		public string ConfigFile
		{
			get
			{
				return this._configFile;
			}
		}
		public string ConfigSection
		{
			get
			{
				return this._configSection;
			}
		}
		public S1Config(string configFile) : this(configFile, "default")
		{
		}
		public S1Config(string configFile, string configSection) : this(configFile, configSection, true)
		{
		}
		public S1Config(string configFile, string configSection, bool useCache) : this(configFile, configSection, useCache, false)
		{
		}
		public S1Config(string configFile, string configSection, bool useCache, bool inheritFromDefault)
		{
			this._configFile = configFile;
			this._configSection = configSection;
			this._useCache = useCache;
			this._inheritFromDefault = (inheritFromDefault && configSection.ToLower() != "default");
		}
		public object GetObject(string key)
		{
			if (!this._useCache)
			{
				S1ConfigLoader.ClearCache();
			}
			string value = S1ConfigLoader.GetValue(this.ConfigFile, this.ConfigSection, key);
			if (value.Length == 0 && this._inheritFromDefault)
			{
				value = S1ConfigLoader.GetValue(this.ConfigFile, "default", key);
			}
			return value;
		}
		public string[] GetKeys()
		{
			if (!this._useCache)
			{
				S1ConfigLoader.ClearCache();
			}
			return S1ConfigLoader.GetKeys(this.ConfigFile, this.ConfigSection);
		}
		public static string[] GetSections(string fileName)
		{
			return S1ConfigLoader.GetSections(fileName);
		}
		public static string[] GetFiles()
		{
			return S1ConfigLoader.GetFiles();
		}
		public static void AddConfigurationPath(string configPath)
		{
			S1ConfigLoader.AddConfigurationPath(configPath, true);
		}
		public static void AddConfigurationPath(string configPath, bool hasPriority)
		{
			S1ConfigLoader.AddConfigurationPath(configPath, hasPriority);
		}
		public static void AddConfigurationFile(string configFile)
		{
			S1ConfigLoader.AddConfigurationFile(configFile);
		}
		public static void AddConfigurationSetting(string file, string section, string setting, string value)
		{
			S1ConfigLoader.AddConfigurationSetting(file, section, setting, value);
		}
		public static void AddOptionalFile(string configFile)
		{
			S1ConfigLoader.AddOptionalFile(configFile);
		}
		public static string[] GetConfigurationPaths()
		{
			return S1ConfigLoader.GetConfigurationPaths();
		}
		public string GetString(string key)
		{
			return this.GetObject(key).ToString();
		}
		public string GetString(string key, string defaultValue)
		{
			return this.GetObject(key).ToString(defaultValue);
		}
		public byte GetByte(string key)
		{
			return this.GetObject(key).ToByte();
		}
		public byte GetByte(string key, byte defaultValue)
		{
			return this.GetObject(key).ToByte(defaultValue);
		}
		public short GetInt16(string key)
		{
			return this.GetObject(key).ToInt16();
		}
		public short GetInt16(string key, short defaultValue)
		{
			return this.GetObject(key).ToInt16(defaultValue);
		}
		public int GetInt32(string key)
		{
			return this.GetObject(key).ToInt32();
		}
		public int GetInt32(string key, int defaultValue)
		{
			return this.GetObject(key).ToInt32(defaultValue);
		}
		public long GetInt64(string key)
		{
			return this.GetObject(key).ToInt64();
		}
		public long GetInt64(string key, long defaultValue)
		{
			return this.GetObject(key).ToInt64(defaultValue);
		}
		public decimal GetDecimal(string key)
		{
			return this.GetObject(key).ToDecimal();
		}
		public decimal GetDecimal(string key, decimal defaultValue)
		{
			return this.GetObject(key).ToDecimal(defaultValue);
		}
		public decimal GetDecimal(string key, string cultureName)
		{
			return this.GetObject(key).ToDecimal(cultureName);
		}
		public decimal GetDecimal(string key, string cultureName, decimal defaultValue)
		{
			return this.GetObject(key).ToDecimal(cultureName, defaultValue);
		}
		public decimal GetDecimal(string key, CultureInfo cultureInfo)
		{
			return this.GetObject(key).ToDecimal(cultureInfo);
		}
		public decimal GetDecimal(string key, CultureInfo cultureInfo, decimal defaultValue)
		{
			return this.GetObject(key).ToDecimal(cultureInfo, defaultValue);
		}
		public float GetSingle(string key)
		{
			return this.GetObject(key).ToSingle();
		}
		public float GetSingle(string key, float defaultValue)
		{
			return this.GetObject(key).ToSingle(defaultValue);
		}
		public float GetSingle(string key, CultureInfo cultureInfo)
		{
			return this.GetObject(key).ToSingle(cultureInfo);
		}
		public float GetSingle(string key, CultureInfo cultureInfo, float defaultValue)
		{
			return this.GetObject(key).ToSingle(cultureInfo, defaultValue);
		}
		public float GetSingle(string key, string cultureName)
		{
			return this.GetObject(key).ToSingle(cultureName);
		}
		public float GetSingle(string key, string cultureName, float defaultValue)
		{
			return this.GetObject(key).ToSingle(cultureName, defaultValue);
		}
		public float GetFloat(string key)
		{
			return this.GetObject(key).ToFloat();
		}
		public float GetFloat(string key, float defaultValue)
		{
			return this.GetObject(key).ToFloat(defaultValue);
		}
		public float GetFloat(string key, CultureInfo cultureInfo)
		{
			return this.GetObject(key).ToFloat(cultureInfo);
		}
		public float GetFloat(string key, CultureInfo cultureInfo, float defaultValue)
		{
			return this.GetObject(key).ToFloat(cultureInfo, defaultValue);
		}
		public float GetFloat(string key, string cultureName)
		{
			return this.GetObject(key).ToFloat(cultureName);
		}
		public float GetFloat(string key, string cultureName, float defaultValue)
		{
			return this.GetObject(key).ToFloat(cultureName, defaultValue);
		}
		public double GetDouble(string key)
		{
			return this.GetObject(key).ToDouble();
		}
		public double GetDouble(string key, double defaultValue)
		{
			return this.GetObject(key).ToDouble(defaultValue);
		}
		public double GetDouble(string key, string cultureName)
		{
			return this.GetObject(key).ToDouble(cultureName);
		}
		public double GetDouble(string key, string cultureName, double defaultValue)
		{
			return this.GetObject(key).ToDouble(cultureName, defaultValue);
		}
		public double GetDouble(string key, CultureInfo cultureInfo)
		{
			return this.GetObject(key).ToDouble(cultureInfo);
		}
		public double GetDouble(string key, CultureInfo cultureInfo, double defaultValue)
		{
			return this.GetObject(key).ToDouble(cultureInfo, defaultValue);
		}
		public bool GetBoolean(string key)
		{
			return this.GetObject(key).ToBoolean();
		}
		public bool GetBoolean(string key, bool defaultValue)
		{
			return this.GetObject(key).ToBoolean(defaultValue);
		}
		public DateTime GetDateTime(string key)
		{
			return this.GetObject(key).ToDateTime();
		}
		public DateTime GetDateTime(string key, DateTime defaultValue)
		{
			return this.GetObject(key).ToDateTime(defaultValue);
		}
		public DateTime GetDateTime(string key, string cultureName)
		{
			return this.GetObject(key).ToDateTime(cultureName);
		}
		public DateTime GetDateTime(string key, string cultureName, DateTime defaultValue)
		{
			return this.GetObject(key).ToDateTime(cultureName, defaultValue);
		}
		public DateTime GetDateTime(string key, CultureInfo cultureInfo)
		{
			return this.GetObject(key).ToDateTime(cultureInfo);
		}
		public DateTime GetDateTime(string key, CultureInfo cultureInfo, DateTime defaultValue)
		{
			return this.GetObject(key).ToDateTime(cultureInfo, defaultValue);
		}
		public TimeSpan GetTimeSpan(string key)
		{
			return this.GetObject(key).ToTimeSpan();
		}
		public TimeSpan GetTimeSpan(string key, TimeSpan defaultValue)
		{
			return this.GetObject(key).ToTimeSpan(defaultValue);
		}
    }
}
